package com.renchunlin.taobaounion.presenter.impl;

import android.util.Log;

import com.renchunlin.taobaounion.model.Api;
import com.renchunlin.taobaounion.model.domain.SelectedContent;
import com.renchunlin.taobaounion.model.domain.SelectedPageCategory;
import com.renchunlin.taobaounion.presenter.ISelectedPagePresenter;
import com.renchunlin.taobaounion.utils.RetrofitManage;
import com.renchunlin.taobaounion.utils.UrlUtils;
import com.renchunlin.taobaounion.view.ISelectedPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/1/22.
 * PS: Not easy to write code, please indicate.
 */
public class SelectedPagePresenterImpl implements ISelectedPagePresenter {

    private ISelectedPageCallback mViewCallback;
    private final Api mApi;

    public SelectedPagePresenterImpl() {
        //拿到retrofit
        Retrofit retrofit = RetrofitManage.getInstance().getRetrofit();
        mApi = retrofit.create(Api.class);
    }

    @Override
    public void getCategories() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }
        Call<SelectedPageCategory> task = mApi.getSelectedPageCategories();
        task.enqueue(new Callback<SelectedPageCategory>() {
            @Override
            public void onResponse(Call<SelectedPageCategory> call, Response<SelectedPageCategory> response) {
                int resultCode = response.code();
                if (resultCode == HttpURLConnection.HTTP_OK) {
                    SelectedPageCategory body = response.body();
                    //通知ui更新
                    if (mViewCallback != null) {
                        mViewCallback.onCategoriesLoaded(body);
                    }
                } else {
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedPageCategory> call, Throwable t) {
                onLoadedError();
            }
        });
    }

    private void onLoadedError() {
        if (mViewCallback != null) {
            mViewCallback.onError();
        }
    }

    @Override
    public void getContentByCategory(SelectedPageCategory.DataBean item) {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }
        String targetUrl = UrlUtils.getSelectedPageContentUrl(item.getFavorites_id());
        Call<SelectedContent> task = mApi.getSelectedPageContent(targetUrl);
        task.enqueue(new Callback<SelectedContent>() {
            @Override
            public void onResponse(Call<SelectedContent> call, Response<SelectedContent> response) {
                int resultCode = response.code();
                if (resultCode == HttpURLConnection.HTTP_OK) {
                    SelectedContent result = response.body();
                    if (mViewCallback != null) {
                        mViewCallback.onContentLoaded(result);
                    }
                } else {
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedContent> call, Throwable t) {
                onLoadedError();
            }
        });
    }

    //重新加载内容
    @Override
    public void reloadContent() {
        this.getCategories();
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback = callback;
    }

    @Override
    public void unregisterViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback = null;
    }
}
